package controller;

import cn.edu.zut.dto.BookAndType;
import cn.edu.zut.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserBookController {
    @Autowired
    BookService bookService;

    //查询所有的图书信息
    @RequestMapping(value = "bookSearch1")
    public String bookSearch1(HttpServletRequest request, Model model){
        List<BookAndType> books = bookService.selectBook();
        request.getSession().setAttribute("bookALL", books);
        return "user/book/bookSearch1";
    }

    //图书模糊查询
    @RequestMapping(value = "checkBook")
    public String checkBook(@RequestParam String selectName, @RequestParam String search,
                            HttpServletRequest request, Model model){
        List<BookAndType> books = new ArrayList<>();
        System.out.println(selectName);
        System.out.println(search);
        switch (selectName){
            case "isbn":books = bookService.selectBookByISBN(search);break;
            case "书名":books = bookService.selectBookByName(search);break;
            case "作者":books = bookService.selectBookByAuthor(search);break;
            case "出版社":books = bookService.selectBookByPublish(search);break;
            case "类别": books = bookService.selectBookAndTypeByTypeid(search);break;
            default:return "user/book/bookSearch1";
        }
        request.getSession().setAttribute("bookALL", books);
        return "user/book/bookSearch1";
    }
}
